package com.examiner.dao;

import com.examiner.bean.CommonPerson;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 一般应虎  dao层
 */
public interface CommonPersonDao extends JpaRepository<CommonPerson,Integer>, JpaSpecificationExecutor<CommonPerson> {

    /**
     * 检测该用户名是否存在
     * @param userName
     * @return
     */
    CommonPerson findByUserName(String userName);

    @Modifying(clearAutomatically = true)
    @Query(value = "update common_person set password=:newPassword where id=:id",nativeQuery = true)
    int updatePassword(@Param("id")Integer id,
                       @Param("newPassword") String newPassword);

    /**
     * 动态删除(改变state)
     * @param commonPersonId
     * @param state
     */
    @Modifying(clearAutomatically = true)
    @Query(value = "update common_person set state=:state where id=:commonPersonId",nativeQuery = true)
    void changeCommonPersonState(@Param("commonPersonId") Integer commonPersonId,
                                 @Param("state") Boolean state);

    /**
     * 根据 superior  获取用户列表
     * @param superior
     * @return
     */
    List<CommonPerson> findBySuperior(String superior);

    /**
     * UPDATE 表名称 SET 列名称 = 新值
     */
    @Modifying(clearAutomatically = true)
    @Query(value = "update common_person SET state=:state,eid=1,examiner='e1'", nativeQuery = true)
    void changeAllState(@Param("state") Boolean state);

    @Modifying(clearAutomatically = true)
    @Query(value = "update common_person SET eid=?2,examiner=?3 where id=?1", nativeQuery = true)
    void changeCommonPersonExaminer(Integer id, Integer eid, String examiner);

    @Query(value = "select id from common_person where user_name like %?1% and user_name != '部门和职位管理'", nativeQuery = true)
    List<Integer> findIdByQuery(String department);

    List<CommonPerson> findBySIdAndStype(Integer id, String stype);


}
